{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "               Open     High      Low    Close      Volume\n",
      "Date                                                      \n",
      "2022-02-18  4384.57  4394.60  4327.22  4348.87  2565520145\n",
      "2022-02-17  4456.06  4456.06  4373.81  4380.26  2308231918\n",
      "2022-02-16  4455.75  4489.55  4429.68  4475.01  2334132066\n",
      "2022-02-15  4429.28  4472.77  4429.28  4471.07  2348139435\n",
      "2022-02-14  4412.61  4426.22  4364.84  4401.67  2520881740\n"
     ]
    }
   ],
   "source": [
    "# 滚动窗口\n",
    "\n",
    "import pandas as pd\n",
    "\n",
    "import pandas_datareader.data as data\n",
    "\n",
    "\n",
    "\n",
    "df = data.get_data_stooq('^SPX')\n",
    "print(df.head())\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "range(30, 40)\n",
      "start = 30\n",
      "stop = 39\n",
      "range(20, 30)\n",
      "start = 20\n",
      "stop = 29\n",
      "range(10, 20)\n",
      "start = 10\n",
      "stop = 19\n",
      "range(0, 10)\n",
      "start = 0\n",
      "stop = 9\n"
     ]
    }
   ],
   "source": [
    "# 滚动窗口\n",
    "\n",
    "win_len = 10 # 窗口长度\n",
    "all_len = 40 # 整体长度\n",
    "\n",
    "# 顺序\n",
    "# for i in range(0, all_len - win_len + 1, win_len):\n",
    "#     print(range(i, i + win_len))\n",
    "#     print('start =', i)\n",
    "#     print('stop =', i + win_len - 1)\n",
    "#     pass\n",
    "\n",
    "\n",
    "\n",
    "# 倒叙\n",
    "# for i in range(all_len - 1, win_len , -win_len):\n",
    "#     print(range(i - win_len + 1, i + 1))\n",
    "#     print('start =', i - win_len + 1)\n",
    "#     print('stop =', i)\n",
    "#     pass\n",
    "\n",
    "for i in range(all_len, win_len - 1 , -win_len):\n",
    "    print(range(i - win_len, i))\n",
    "    print('start =', i - win_len)\n",
    "    print('stop =', i - 1)\n",
    "    pass\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
  },
  "kernelspec": {
   "display_name": "Python 3.7.3 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
